from pic_utils import pic_tools,batch_manager,txt_tools
from insects_utils import insects_tools
import sys
import time
import numpy as np
import cv2 as cv
import os
import math
import time
from project_utils import project_tools
import matplotlib.pyplot as plt
from model_utils import model_tools
from sklearn.svm import LinearSVC

#准备工作文件夹
project_root_dir=project_tools.project_root_path()
project_tools.prepare_working_dirs(project_root_dir)
#设定文件读取路径
gallery_path="D:\\1Learning\江苏大学\虫害识别图像数据\四月份数据"
# gallery_path = project_root_dir+'\\'+project_tools.insects_img_dirname
processed_gallery_path=project_root_dir+'\\'+project_tools.processed_img_dirname
chas_data_path=project_root_dir+'\\'+project_tools.characteristic_dirname
model_path=project_root_dir+'\\'+project_tools.models_dirname+'\\'+project_tools.model_name
#测试代码
# store_folder_path=pic_tools.write_all_processed_imgs(gallery_path,processed_gallery_path)
# pic_tools.rename_imgs(store_folder_path,'1','.jpg')
#查看计数的效果
# insects_tools.insects_count_visualization(processed_gallery_path+'\\'+'1-7-1-B.jpg')
#获取温室大棚病虫害计数信息
# start=time.perf_counter()
# if pic_tools.read_imgs_from_dir(processed_gallery_path)==[]:
#     pic_tools.write_all_processed_imgs(gallery_path,processed_gallery_path)
#     print(insects_tools.green_house_insects_info(processed_gallery_path))
# else:
#     print(insects_tools.green_house_insects_info(processed_gallery_path))
# end=time.perf_counter()
# print('Running time: %s Seconds'%(float(end-start)))

#获取所有图片上昆虫的特征矩阵
# pic_tools.extract_all_imgs_characteristcs('D:\python\workspace\insects_detection\processed_imgs\四月份数据',chas_data_path)

# 虫种学习模块
# img_path='D:\python\workspace\insects_detection\processed_imgs\\5-2-1-A.jpg'
# img=pic_tools.read_imgs(img_path)
# filename = os.path.splitext(img_path)[0].rsplit('\\', 1)[1]
# insects_chas,insects_tags=pic_tools.extract_single_insect_img(img)
# pic_tools.save_insects_tags(insects_tags,filename)
# pic_tools.save_random_extracted_insects_chas(insects_chas,filename)

#构建虫害识别模型
# base_insects_chas,base_insects_tags=model_tools.merge_insects_chas_and_tags([
#      'D:\python\workspace\insects_detection\insects_tags\\1-1-3-B.txt',
#      'D:\python\workspace\insects_detection\insects_tags\\1-1-4-B.txt',
#      'D:\python\workspace\insects_detection\insects_tags\\1-2-4-A.txt',
#      'D:\python\workspace\insects_detection\insects_tags\\1-8-4-B.txt',
#      'D:\python\workspace\insects_detection\insects_tags\\1-9-2-A.txt',
#      'D:\python\workspace\insects_detection\insects_tags\\1-9-3-A.txt',
#      ])
# model=LinearSVC(max_iter=1000000,C=300)
# model_tools.construct_model(base_insects_chas,base_insects_tags,model,'Model4April')
# 获取温室大棚病虫害分类计数信息
scaler_path='D:\python\workspace\insects_detection\\trainning_models\Model4April_scaler.pkl'
print(insects_tools.classify_green_house_insects('D:\python\workspace\insects_detection\chas_data\四月份数据',model_path,scaler_path))
# 功能测试区
# print(insects_tools.classify_insects_on_single_chas_file('D:\python\workspace\insects_detection\chas_data\\1-1-3-B.txt','D:\python\workspace\insects_detection\\trainning_models\\Model4April.pkl',scaler_path))


